package com.bill99.golden.inf.sso.session;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bill99.golden.inf.sso.model.SessionItem;
import com.bill99.golden.inf.sso.util.SSOUtil;

public class SessionService {
	private static Log log = LogFactory.getLog(SessionService.class);
	private static final Map<String, Map<String, SessionItem>> sessionMap = new ConcurrentHashMap<String, Map<String, SessionItem>>();
	private static final int sessionHours = 3;
	private static final Map<String, Long> tokenTime = new ConcurrentHashMap<String, Long>();

	public static void updateTokenTime(String token) {
		if (SSOUtil.isValidateToken(token))
			tokenTime.put(token, new Long(System.currentTimeMillis()));
	}

	public static LocalSession getLocalSession(String token) {
		if (SSOUtil.isValidateToken(token) == false)
			return null;
		Map<String, SessionItem> m = sessionMap.get(token);
		if (m == null) {
			m = new ConcurrentHashMap<String, SessionItem>();
			sessionMap.put(token, m);
		}
		return new LocalSessionImpl(token, m);
	}

	public static void destroySession(String token) {
		if (token == null)
			return;
		Map<String, SessionItem> m = sessionMap.get(token);
		if (m != null) {
			sessionMap.remove(token);
			Iterator<String> it = m.keySet().iterator();
			while (it.hasNext()) {
				String name = it.next();
				m.remove(name);
				System.out.println("remove name=" + name);
			}
		}
	}

	public static void checkTimeoutSessionObject() {
		log.debug("checkTimeoutSessionObject start...............");
		Iterator<String> it = sessionMap.keySet().iterator();
		int cnt = 0;
		while (it.hasNext()) {
			String token = it.next();
			Long time = tokenTime.get(token);
			if (time != null) {
				int hour = (int) ((System.currentTimeMillis() - time
						.longValue()) / 1000 / 3600);
				if (hour >= sessionHours) {
					SessionService.destroySession(token);
					cnt++;
				}
			} else {
				SessionService.destroySession(token);
				cnt++;
			}
		}
		log.debug("checkTimeoutSessionObject end..........destroy session cnt="
				+ cnt);
	}

	public static int getLocalSessionCount() {
		return sessionMap.size();
	}

	public static int getTotalSessionObjectCount() {
		int cnt = 0;
		Iterator<Map<String, SessionItem>> m = sessionMap.values().iterator();
		while (m.hasNext()) {
			Map<String, SessionItem> map = m.next();
			cnt += map.size();
		}
		return cnt;
	}

	public static void main(String[] args) {
		LocalSession s = SessionService.getLocalSession("UJJHHJ876");
		for (int i = 0; i < 40; i++) {
			s.setAttribute("name" + i, new SessionItem("kkk=" + i, null));
		}
		s = SessionService.getLocalSession("HJAJKAJKJ");
		for (int i = 0; i < 14; i++) {
			s.setAttribute("nameA" + i, new SessionItem("kkk999=" + i, null));
		}
		SessionService.destroySession("UJJHHJ876");
		System.out.println("sessions=" + SessionService.getLocalSessionCount()
				+ " oCnt=" + SessionService.getTotalSessionObjectCount());
	}
}
